package com.example.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.admin_model.entity.Employee;
import com.example.common.Result;
import com.example.admin.service.EmployeeService;
import com.example.config.JwtConfig;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author sjz
 * @since 2024-06-26
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;


    /**
     * 登录
     *
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody Employee employee) {

        String token = employeeService.login(employee);
        return Result.success(token);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        log.info("退出成功");
        return Result.success("退出成功!");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Employee> getById(@PathVariable("id") String id) {
        log.info("根据id查询对象,id:{}", id);
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            employee.setPassword(null);
        }
        return Result.success(employee);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Employee>> getPage(long page, long pageSize, String name) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //对name进行模糊查询
        wrapper.like(name != null, Employee::getName, name);
        return Result.success(employeeService.page(new Page<>(page, pageSize), wrapper));
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @PostMapping()
    public Result<Object> add(@RequestBody Employee employee) {
        employeeService.save(employee);
        return Result.success();
    }


    /**
     * 更新员工信息
     *
     * @param employee
     * @return
     */
    @PutMapping()
    public Result<Object> update(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return Result.success();
    }

}
